package com.woniu.p2p.serviceImpl;

import com.woniu.p2p.entity.User;
import com.woniu.p2p.entity.UserCharge;
import com.woniu.p2p.mapper.UserChargeMapper;
import com.woniu.p2p.mapper.UserMapper;
import com.woniu.p2p.service.P2PUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 86152
 * @create 2020/8/6
 * @since 1.0.0
 */
@Service
public class P2PUserServiceImp implements P2PUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserChargeMapper userChargeMapper;

    @Override
    public User findUserByName(String uname) {
        return userMapper.findUserByName(uname);
    }

    @Override
    public User findMobile(String userPhone) {
        return userMapper.findMobile(userPhone);
    }

    @Override
    public User findUserCode(Integer userCode) {
        return userMapper.findUserCode(userCode);
    }

    @Override
    public void addIntergal(Integer userId) {
        userMapper.addIntergal(userId);
    }

    @Override
    @Transactional
    public int addUser(User user) {
        Integer userCode = user.getUserCode();
        if(userCode!=null){
            User userByCode = findUserCode(userCode);
            addIntergal(userByCode.getUserId());
        }
        int vercode = (int) ((Math.random() * 9 + 1) * 1000);
        user.setUserCode(vercode);
        int row=userMapper.addUser(user);
        return row;
    }

    //支付宝充值
    @Override
    @Transactional
    public void charge(String orderId, Date date, String orderAmount, String chargeType, String chargeStatus, String chargeContext, int userid) {
        userMapper.addAmountById(userid,Long.parseLong(orderAmount));
        UserCharge userCharge = new UserCharge();
        userCharge.setChargeOrder(orderId);
        userCharge.setChargeTime(date);
        userCharge.setChargeMoney(Long.parseLong(orderAmount));
        userCharge.setChargeType(chargeType);
        userCharge.setChargeStatus(chargeStatus);
        userCharge.setChargeContext(chargeContext);
        userCharge.setUserid(userid);
        userChargeMapper.insert(userCharge);
    }
}
